const Koa = require('koa');
//Application
const app = new  Koa();
//路由
const router = require('koa-router')();
const bodyParser = require('koa-bodyparser');

app.use(bodyParser());

/**
 * 主页
 * @type {[type]}
 */
router.get('/',async (ctx,next) => {
  ctx.response.body = `<h1>Index</h1>
         <form action="/login" method="post">
             <p>Name: <input name="name" value="bzy"></p>
             <p>Password: <input name="password" type="password"></p>
             <p><input type="submit" value="Submit"></p>
         </form>`
         ;
});

/**
 * 登录Api
 * @type {String}
 */
router.post('/login',async (ctx,next) => {
  let loginName = (ctx.response.body && ctx.response.body.loginName) || '',
      password = (ctx.response.body && ctx.response.body.password) || '';

  if(loginName === 'bzy' && password === '12345'){
    console.log('login successfull!')
    ctx.response.body = `欢迎回来: ${loginName} !`;
  }else{
    console.log('login fail!')
    ctx.response.body = '登录失败,请检查用户名和密码!';
  }
});

//使用路由
app.use(router.routes());

//开启服务,监听3001端口
app.listen(3001);
